#include "pcomc660.h"

MODULE_AUTHOR("Tan xujia");
MODULE_LICENSE("Dual BSD/GPL");


struct  pcomc660_dev {
    int major;
    int minor;
    dev_t dev_id;/*设备的主次设备号*/
    struct cdev cdev;
    struct class *class;
    struct device *device;
    void *private_data;
};

static struct pcomc660_dev pcomc660dev;

static int
pcomc660_read_regs(struct pcomc660_dev *dev, u8 reg, void *val, int len)
{
    int rc = 0;
    struct i2c_msg msg[2];
    struct i2c_client *client = (struct i2c_client *)dev->private_data;

    msg[0].addr = client->addr;
    msg[0].flags = 0;
    msg[0].buf = &reg;
    msg[0].len = 1;

    msg[1].addr = client->addr;
    msg[1].flags = I2C_M_RD;/*表示读操作*/
    msg[1].buf = val;
    msg[1].len = len;

    rc = i2c_transfer(client->adapter, msg, 2);
    if(rc == 2) {
        return 0;
    }else {
        return -EREMOTEIO;
    }
}

static int
pcomc660_write_regs(struct pcomc660_dev *dev, u8 reg, u8 *buf, u8 len)
{
    u8 data[256];
    struct i2c_msg msg;
    struct i2c_client *client = (struct i2c_client *)dev->private_data;

    data[0] = reg; /*要写的寄存器*/
    memcpy(&data[1], buf, len);/*要写的值*/

    msg.addr = client->addr;
    msg.flags = 0;/*表示写操作*/
    msg.buf = data;
    msg.len = len + 1;/*len以字节byte为单位*/
    
    return i2c_transfer(client->adapter, &msg, 1);
}

static unsigned char 
pcomc660_read_reg(struct pcomc660_dev *dev, u8 reg)
{
    u8 data = 0;
    pcomc660_read_regs(dev, reg, &data, 1);
    return data;
#if 0
    struct i2c_client *client = (struct i2c_client *)dev->private_data;
    return i2c_smbus_read_byte_data(client, reg);
#endif
}

static void 
pcomc660_write_reg(struct pcomc660_dev *dev, u8 reg, u8 data)
{
    u8 buf = 0;
    buf = data;
    pcomc660_write_regs(dev, reg, &buf, 1);
}

static int 
pcomc660_open(struct inode *inode, struct file *filp)
{
    filp->private_data = &pcomc660dev;
    printk("pcomc660_open\n");

    //TODO:设备寄存器初始化
    return 0;
}

ssize_t 
pcomc660_read(struct file *filp,  char __user *buf, size_t count, loff_t *off)
{
    int rc = 0;
    struct pcomc660_dev *dev = (struct pcomc660_dev*)filp->private_data;
    //TODO:
    printk("pcomc660_read\n");
    return 0;
}

ssize_t 
pcomc660_write(struct file *filp, const char  __user *buf, size_t count, loff_t *fpos)
{
    int rc = 0;
    struct pcomc660_dev *dev = (struct pcomc660_dev*)filp->private_data;
    //TODO:
    printk("pcomc660_write\n");
    return 0;
}

static int
pcomc660_release(struct inode *inode, struct file *filp)
{
    struct pcomc660_dev *dev = (struct pcomc660_dev*)filp->private_data;
    printk("pcomc660_release\n");
    return 0;
}

static const struct file_operations pcomc660_fops = {
    .owner = THIS_MODULE,
    .open = pcomc660_open,
    .read = pcomc660_read,
    .write = pcomc660_write,
    .release = pcomc660_release,
};

static int 
pcomc660_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
    int rc = 0;
    printk("pcomc660_probe\n");
    rc = alloc_chrdev_region(&pcomc660dev.dev_id, 
        0, 1, "iic_dev");
    if(rc) {
        printk("alloc_chrdev_region fail\n");
        goto alloc_cdev_fail;
    }
        
    pcomc660dev.major = MAJOR(pcomc660dev.dev_id);
    pcomc660dev.minor = MINOR(pcomc660dev.dev_id);

    pcomc660dev.cdev.owner = THIS_MODULE;
    cdev_init(&pcomc660dev.cdev, &pcomc660_fops);
    rc = cdev_add(&pcomc660dev.cdev, pcomc660dev.dev_id, 1);
    if(rc) {
        printk("cdev_add fail\n");
        goto cdev_add_fail;
    }

    pcomc660dev.class = class_create(THIS_MODULE, "c_dev");
    if(IS_ERR(pcomc660dev.class)) {
        rc = PTR_ERR(pcomc660dev.class);
        goto class_fail;
    }
    pcomc660dev.device = device_create(pcomc660dev.class, NULL,
        pcomc660dev.dev_id, NULL, "c_dev");
    if(IS_ERR(pcomc660dev.device)) {
        rc = PTR_ERR(pcomc660dev.device);
        goto create_fail;
    }
    pcomc660dev.private_data = client;
create_fail:
    class_destroy(pcomc660dev.class);
class_fail:
    cdev_del(&pcomc660dev.cdev);
alloc_cdev_fail:
    unregister_chrdev_region(pcomc660dev.dev_id ,1);
cdev_add_fail:
    return rc;   
}

static int
pcomc660_remove(struct i2c_client *client)
{
    device_destroy(pcomc660dev.class, pcomc660dev.dev_id);
    class_destroy(pcomc660dev.class);
    cdev_del(&pcomc660dev.cdev);
    unregister_chrdev_region(pcomc660dev.dev_id ,1);     
    return 0;
}

/*常规匹配*/
static struct i2c_device_id pcomc660_id[] = {
    {"pcomc660, 660", 0},
    {}
};

/*设备树匹配*/
static struct of_device_id pcomc660_of_match[] = {
	{.compatible = "pcomc660, 660"},
	{}
};

static struct i2c_driver pcomc660_driver = {
    .probe = pcomc660_probe,
    .remove = pcomc660_remove,
    .driver = {
        .name = "pcomc660",
        .owner = THIS_MODULE,
        .of_match_table = pcomc660_of_match,
    },
    .id_table = pcomc660_id,
};

static int 
__init pcomc660_init(void)
{
	return i2c_add_driver(&pcomc660_driver);
}

static void 
__exit pcomc660_exit(void)
{
    i2c_del_driver(&pcomc660_driver);
}

module_init(pcomc660_init);
module_exit(pcomc660_exit);
